package com.mz.ieann.aide.city.model

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.mz.ieann.aide.base.vm.BaseViewModel
import com.mz.ieann.aide.city.api.DaoService
import com.mz.ieann.aide.city.bean.*

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz90
 * @date on 2021 2021/11/14 13:39
 */
class HouseViewModel : BaseViewModel() {

    private val service by lazy {
        DaoService()
    }

    /**
     * 是否正在查询数据
     */
    var isLoading = false

    fun queryList(
        param: HouseParam,
        owner: LifecycleOwner,
        success: (result: BaseResult<HouseResult>) -> Unit,
        error: () -> Unit = {}
    ){
        isLoading = true
        val listData = MutableLiveData<BaseResult<HouseResult>>()
        request(listData) {
            service.queryList(param, listData, error, { onComplete() })
        }.observe(owner) {
            success(it)
        }
    }

    fun queryDetail(
        cityId : String,
        body : CreateWifiParam,
        owner: LifecycleOwner,
        success: (result: BaseResult<HeatMapResult>) -> Unit,
        error: () -> Unit = {}
    ){
        isLoading = true
        val detailData = MutableLiveData<BaseResult<HeatMapResult>>()
        request(detailData){
            service.queryDetail(cityId, body, detailData, error, { onComplete() })
        }.observe(owner) {
            success(it)
        }
    }

    private fun onComplete(){
        isLoading = false
    }

}